package skeleton
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class VisualKeyboard
	{
		private var _container:DisplayObjectContainer;
		private var _root:Sprite = new Sprite();
		private var _scale:Number = 1;
		
		public function VisualKeyboard(container:DisplayObjectContainer)
		{
			_container = container;
			_container.addChild(_root);
		}
		
		public function addButton(display:DisplayObject, centerX:Number, centerY:Number, keyCode:int, cbAddFilter:Function = null):Sprite
		{
			var node:Sprite = new Sprite();
			_root.addChild(node);
			
			node.addChild(display);
			node.x = centerX;
			node.y = centerY;
			display.x =  -display.width / 2;
			display.y =  -display.height / 2;

			node.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			node.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			node.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			node.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			
			return node;
			
			function onMouseDown(e:MouseEvent):void
			{
				addFilter();
				csKey.setStatus(keyCode, 1);
			}
			
			function onMouseUp(e:MouseEvent):void
			{
				removeFilter();
				csKey.setStatus(keyCode, 0);
			}

			function onRollOver(e:MouseEvent):void
			{
				if(e.buttonDown)
				{
					addFilter();
					csKey.setStatus(keyCode, 1);
				}
				else
				{
					removeFilter();
					csKey.setStatus(keyCode, 0);
				}
			}

			function onRollOut(e:MouseEvent):void
			{
				removeFilter();
				csKey.setStatus(keyCode, 0);
			}
			
			function addFilter():void
			{
				if(cbAddFilter != null)
				{
					cbAddFilter(display);
				}
			}
			
			function removeFilter():void
			{
				display.filters = [];
			}
			
		}
		
		public function setAlpha(alpha:Number):void
		{
			_root.alpha = alpha;
		}
		
		public function setVisible(v:Boolean):void
		{
			_root.visible = v;
		}
		
		public function setScale(scale:Number):void
		{
			if(_scale != scale)
			{
				_scale = scale;
				
				var n:int = _root.numChildren;
				for(var i:int = 0; i < n; ++i)
				{
					var node:DisplayObject = _root.getChildAt(i);
					node.scaleX = scale;
					node.scaleY = scale;
				}
			}
		}
		
	}
}
	